Namespace Core
  Public Class Constants
    Public Const PFX_SCHEMA As String = "eyeLog"
    Public Const PFX_OLD As String = "old"
    Public Const PFX_NEW As String = "new"
    Public Const COL_KEY As String = PFX_SCHEMA & "GuidKey"
    Public Const COL_CREATED_ON As String = PFX_SCHEMA & "CreatedOn"
    Public Const COL_CREATED_BY_USER As String = PFX_SCHEMA & "CreatedByUser"
    Public Const COL_CREATED_BY_APPLICATION As String = PFX_SCHEMA & "CreatedByApplication"
    Public Const COL_CREATED_BY_MACHINE As String = PFX_SCHEMA & "CreatedByMachine"
    Public Const COL_UPDATED_ON As String = PFX_SCHEMA & "UpdatedOn"
    Public Const COL_UPDATED_BY_USER As String = PFX_SCHEMA & "UpdatedByUser"
    Public Const COL_UPDATED_BY_APPLICATION As String = PFX_SCHEMA & "UpdatedByApplication"
    Public Const COL_UPDATED_BY_MACHINE As String = PFX_SCHEMA & "UpdatedByMachine"
    Public Const COL_UPDATED_AS As String = PFX_SCHEMA & "UpdatedAs"
    Public Const PFX_TRI As String = PFX_SCHEMA & "_TRI_"
    Public Const PFX_TRU As String = PFX_SCHEMA & "_TRU_"
    Public Const PFX_TRD As String = PFX_SCHEMA & "_TRD_"

    Public Const PFX_TRIGGER_DDL As String = PFX_SCHEMA & "_TRIGGER_"
    Public Const SFX_TRIGGER_DDL As String = "_DATABASE_EVENTS"

    Public Const COL_ID As String = PFX_SCHEMA & "Id"
    Public Const COL_EVENT_TYPE As String = PFX_SCHEMA & "EventType"
    Public Const COL_POST_TIME As String = PFX_SCHEMA & "PostTime"
    Public Const COL_SPID As String = PFX_SCHEMA & "SPID"
    Public Const COL_SERVER_NAME As String = PFX_SCHEMA & "ServerName"
    Public Const COL_LOGIN_NAME As String = PFX_SCHEMA & "LoginName"
    Public Const COL_USER_NAME As String = PFX_SCHEMA & "UserName"
    Public Const COL_DATABASE_NAME As String = PFX_SCHEMA & "DatabaseName"
    Public Const COL_SCHEMA_NAME As String = PFX_SCHEMA & "SchemaName"
    Public Const COL_OBJECT_NAME As String = PFX_SCHEMA & "ObjectName"
    Public Const COL_OBJECT_TYPE As String = PFX_SCHEMA & "ObjectType"
    Public Const COL_COMMAND_TEXT As String = PFX_SCHEMA & "CommandText"
    Public Const COL_RAW_DATA As String = PFX_SCHEMA & "RawData"

    Private Sub New()
    End Sub

    Public Shared Sub AddScriptItem(ByVal script As List(Of String), ByVal item As String)
      If item IsNot Nothing AndAlso item.Length > 0 Then script.Add(item)
    End Sub

    Public Shared Function GetScriptDropObject(ByVal schema As String, ByVal name As String, ByVal type As ObjectTypes) As String
      Dim rc As String

      Select Case type
        Case ObjectTypes.Trigger
          ' rc = String.Format("IF EXISTS (SELECT * FROM sysObjects WHERE xtype='T' AND name='[{1}]{3}[{2}]') {0}DROP TRIGGER [{1}]{3}[{2}]", vbCrLf, schema, name, IIf(useSchema = True, ".", "_"))
          rc = String.Format("IF EXISTS (SELECT * FROM sysObjects WHERE xtype='TR' AND id=OBJECT_ID('[{1}].[{2}]')) {0}DROP TRIGGER [{1}].[{2}]", vbCrLf, schema, name)
        Case ObjectTypes.Table
          rc = String.Format("IF EXISTS (SELECT * FROM sysObjects WHERE xtype='U' AND id=OBJECT_ID('[{1}].[{2}]')) {0}DROP TABLE [{1}].[{2}];", vbCrLf, schema, name)
        Case ObjectTypes.DdlTrigger
          rc = String.Format("IF  EXISTS (SELECT * FROM sys.triggers WHERE name = N'{2}' AND parent_class=0) DROP TRIGGER [{2}] ON DATABASE;", vbCrLf, schema, name)
        Case Else
          rc = Nothing
#If DEBUG Then
          Stop
#End If
      End Select

      Return rc
    End Function

    <Flags()> Public Enum Editions
      Personal = 1
      Standard = 2
      Professional = 4
      IPKG = 8
    End Enum

    Public Enum ObjectTypes
      None
      Column
      Constraint
      Table
      Trigger
      DdlTrigger
    End Enum

    Public Enum SqlVersion
      None
      Sql2000 = 2000
      Sql2005 = 2005
    End Enum

    <Flags()> Public Enum ColumnStatuses
      None = 0
      Installed = 1
    End Enum

    <Flags()> Public Enum ColumnActions
      None
      Install
      Remove
    End Enum

    <Flags()> Public Enum LogActions
      None = 0
      Insert = 1
      Update = 2
      Delete = 4
    End Enum

    <Flags()> Public Enum TableStatuses
      None = 0
      Minimum = 1 ' Light version: only system fields in audited table
      Standard = 2 ' History table
      Complete = 3 ' System fields + history table
    End Enum

    <Flags()> Public Enum TableActions
      None = 0
      InstallMinimum = 1
      InstallStandard = 2
      InstallComplete = 4
      Remove = 7
    End Enum

    <Flags()> Public Enum DatabaseStatuses
      None = 0
      Installed = 1
    End Enum
  End Class

  <Flags()> _
  Public Enum DDL_EVENTS As ULong
    DDL_DATABASE_LEVEL_EVENTS = DDL_TABLE_VIEW_EVENTS + DDL_SYNONYM_EVENTS + DDL_FUNCTION_EVENTS + DDL_PROCEDURE_EVENTS + DDL_TRIGGER_EVENTS + DDL_EVENT_NOTIFICATION_EVENTS + DDL_ASSEMBLY_EVENTS + DDL_TYPE_EVENTS + DDL_DATABASE_SECURITY_EVENTS

    DDL_TABLE_VIEW_EVENTS = DDL_TABLE_EVENTS + DDL_VIEW_EVENTS + DDL_INDEX_EVENTS + DDL_STATISTICS_EVENTS

    DDL_TABLE_EVENTS = CREATE_TABLE + ALTER_TABLE + DROP_TABLE
    CREATE_TABLE = 2 ^ 0
    ALTER_TABLE = 2 ^ 1
    DROP_TABLE = 2 ^ 2

    DDL_VIEW_EVENTS = CREATE_VIEW + ALTER_VIEW + DROP_VIEW
    CREATE_VIEW = 2 ^ 3
    ALTER_VIEW = 2 ^ 4
    DROP_VIEW = 2 ^ 5

    DDL_INDEX_EVENTS = CREATE_INDEX + ALTER_INDEX + DROP_INDEX + CREATE_XML_INDEX
    CREATE_INDEX = 2 ^ 6
    ALTER_INDEX = 2 ^ 7
    DROP_INDEX = 2 ^ 8
    CREATE_XML_INDEX = 2 ^ 9

    DDL_STATISTICS_EVENTS = CREATE_STATISTICS + UPDATE_STATISTICS + DROP_STATISTICS
    CREATE_STATISTICS = 2 ^ 10
    UPDATE_STATISTICS = 2 ^ 11
    DROP_STATISTICS = 2 ^ 12

    DDL_SYNONYM_EVENTS = CREATE_SYNONYM + DROP_SYNONYM
    CREATE_SYNONYM = 2 ^ 13
    DROP_SYNONYM = 2 ^ 14

    DDL_FUNCTION_EVENTS = CREATE_FUNCTION + ALTER_FUNCTION + DROP_FUNCTION
    CREATE_FUNCTION = 2 ^ 15
    ALTER_FUNCTION = 2 ^ 16
    DROP_FUNCTION = 2 ^ 17

    DDL_PROCEDURE_EVENTS = CREATE_PROCEDURE + ALTER_PROCEDURE + DROP_PROCEDURE
    CREATE_PROCEDURE = 2 ^ 18
    ALTER_PROCEDURE = 2 ^ 19
    DROP_PROCEDURE = 2 ^ 20

    DDL_TRIGGER_EVENTS = CREATE_TRIGGER + ALTER_TRIGGER + DROP_TRIGGER
    CREATE_TRIGGER = 2 ^ 21
    ALTER_TRIGGER = 2 ^ 22
    DROP_TRIGGER = 2 ^ 23

    DDL_EVENT_NOTIFICATION_EVENTS = CREATE_EVENT_NOTIFICATION + DROP_EVENT_NOTIFICATION
    CREATE_EVENT_NOTIFICATION = 2 ^ 24
    DROP_EVENT_NOTIFICATION = 2 ^ 25

    DDL_ASSEMBLY_EVENTS = CREATE_ASSEMBLY + ALTER_ASSEMBLY + DROP_ASSEMBLY
    CREATE_ASSEMBLY = 2 ^ 26
    ALTER_ASSEMBLY = 2 ^ 27
    DROP_ASSEMBLY = 2 ^ 28

    DDL_TYPE_EVENTS = CREATE_TYPE + DROP_TYPE
    CREATE_TYPE = 2 ^ 29
    DROP_TYPE = 2 ^ 30

    DDL_DATABASE_SECURITY_EVENTS = DDL_CERTIFICATE_EVENTS + DDL_USER_EVENTS + DDL_ROLE_EVENTS + DDL_APPLICATION_ROLE_EVENTS + DDL_SCHEMA_EVENTS + DDL_GDR_DATABASE_EVENTS + DDL_AUTHORIZATION_DATABASE_EVENTS

    DDL_CERTIFICATE_EVENTS = CREATE_CERTIFICATE + ALTER_CERTIFICATE + DROP_CERTIFICATE
    CREATE_CERTIFICATE = 2 ^ 31
    ALTER_CERTIFICATE = 2 ^ 32
    DROP_CERTIFICATE = 2 ^ 33

    DDL_USER_EVENTS = CREATE_USER + ALTER_USER + DROP_USER
    CREATE_USER = 2 ^ 34
    ALTER_USER = 2 ^ 35
    DROP_USER = 2 ^ 36

    DDL_ROLE_EVENTS = CREATE_ROLE + ALTER_ROLE + DROP_ROLE
    CREATE_ROLE = 2 ^ 37
    ALTER_ROLE = 2 ^ 38
    DROP_ROLE = 2 ^ 39

    DDL_APPLICATION_ROLE_EVENTS = CREATE_APPROLE + ALTER_APPROLE + DROP_APPROLE
    CREATE_APPROLE = 2 ^ 40
    ALTER_APPROLE = 2 ^ 41
    DROP_APPROLE = 2 ^ 42

    DDL_SCHEMA_EVENTS = CREATE_SCHEMA + ALTER_SCHEMA + DROP_SCHEMA
    CREATE_SCHEMA = 2 ^ 43
    ALTER_SCHEMA = 2 ^ 44
    DROP_SCHEMA = 2 ^ 45

    DDL_GDR_DATABASE_EVENTS = GRANT_DATABASE + DENY_DATABASE + REVOKE_DATABASE
    GRANT_DATABASE = 2 ^ 46
    DENY_DATABASE = 2 ^ 47
    REVOKE_DATABASE = 2 ^ 48

    ' When there is only one Child, a duplicate key exception occurs
    DDL_AUTHORIZATION_DATABASE_EVENTS = 2 ^ 49 'ALTER_AUTHORIZATION_DATABASE
    'ALTER_AUTHORIZATION_DATABASE = 2 ^ 49

  End Enum
End Namespace
